//
//  HomeViewController.m
//  Ten
//
//  Created by anfuer on 2018/8/14.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "HomeViewController.h"
#import "collCell1.h"
#import "collCell2.h"
#import "HomeTbvCell.h"
#import "SDCycleScrollView.h"
#import "YMCitySelect.h"
#import "YSLocationManager.h"
#import "ShopHome2Controller.h"

@interface HomeViewController ()<SDCycleScrollViewDelegate,YMCitySelectDelegate,UIScrollViewDelegate>
{
    SDCycleScrollView *cycleScrollView;
    NSArray*colldate1;
    NSArray*company;
    int page;
    NSMutableArray*mary;
    NSArray*banner;
    NSArray*menu;
    NSArray*index_job_ads;
}
@property (weak, nonatomic) IBOutlet UIView *navback;
@property (weak, nonatomic) IBOutlet UIView *bannerBack;
@property (weak, nonatomic) IBOutlet UICollectionView *collview1;
@property (weak, nonatomic) IBOutlet UICollectionView *collview2;
@property (weak, nonatomic) IBOutlet UITableView *tbv;
@property (weak, nonatomic) IBOutlet UIButton *cityBtno;

@end

@implementation HomeViewController
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

-(void)ym_ymCitySelectCityName:(NSString *)cityName{
    
    [_cityBtno setTitle:cityName forState:UIControlStateNormal];
    
}

- (IBAction)CityBtna:(UIButton *)sender {
    
     [self presentViewController:[[YMCitySelect alloc] initWithDelegate:self] animated:YES completion:nil];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView==_tbv) {
        
        _navback.backgroundColor = [[UIColor groupTableViewBackgroundColor] colorWithAlphaComponent:scrollView.contentOffset.y>230?1:0];
        if(scrollView.contentOffset.y>230)
        {
             [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
            [_cityBtno setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        }else
        {
           [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
            [_cityBtno setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[YSLocationManager sharedManager] getLocationDidFinishBlock:^(YSLocationManagerModel *model) {
        [_cityBtno setTitle:model.locality forState:UIControlStateNormal];
        
    } errorBolck:^(NSError *error) {
        [WSProgressHUD showImage:nil status:@"定位失败"];
    }];
//    [FuncPublic getData:self parameters:nil RequestUrl:XgetCitys success:^(NSDictionary *acountInfo) {
//        NSLog(@"%@",acountInfo);
//    }];

    
    
    _tbv.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        page=0;
        mary=[NSMutableArray array];
        [self loaddata];
    }];
    
    [_tbv.mj_header beginRefreshing];
    
    
    colldate1=@[@"工业制造",@"文化娱乐",@"影视拍摄",@"推广服务",@"居民日常",@"技术服务",@"培训服务",@"设计服务",@"信息服务",@"物流配送",@"咨询服务",@"更多"];
     _navback.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    
    if (!cycleScrollView) {
        cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEVW, 230) delegate:self placeholderImage:WXGImage(@"banner占位")];
        [_bannerBack addSubview:cycleScrollView];
        cycleScrollView.autoScrollTimeInterval=5;
        
    }
    
    if (@available(iOS 11.0, *)){
        _tbv.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tbv.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    }else
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _tbv.separatorColor=[UIColor clearColor];
    // Do any additional setup after loading the view.
}

-(void)loaddata
{
    [FuncPublic getData:self parameters:@{@"page":[NSString stringWithFormat:@"%d",page]} RequestUrl:Cindex_new success:^(NSDictionary *acountInfo) {
        [_tbv.mj_header endRefreshing];
        [_tbv.mj_footer endRefreshing];
         banner =acountInfo[@"banner"];
        if (page==0) {
            menu=acountInfo[@"menu"];
            index_job_ads=acountInfo[@"index_job_ads"];
            [_collview1 reloadData];
            [_collview2 reloadData];
        }
      
        NSMutableArray*tempmary=[NSMutableArray array];
        [banner enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary*dic=obj;
            [tempmary addObject:dic[@"image"]];
        }];
          cycleScrollView.imageURLStringsGroup=tempmary;

          NSArray*tempary=acountInfo[@"job"];
     
          
            if (tempary.count) {
               [mary addObjectsFromArray:tempary];
                _tbv.mj_footer=[MJRefreshAutoFooter footerWithRefreshingBlock:^{
                    page++;
                    [self loaddata];
                }];
            }else
            {
                _tbv.mj_footer=nil;
            }
        [_tbv reloadData];
    }];

}
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"%@",WXGStr(banner[index][@"url"]));
    
    if (![WXGStr(banner[index][@"url"]) empty])[MyUtils OpenHtmlforStr:WXGStr(banner[index][@"url"])];
    
}
- (IBAction)MoreBtna:(UIButton *)sender {
    [MyUtils collviewSelidex:sender.tag==1?@"名企推荐":@"全职" toVc:self andShopInfo:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma  mark Coll相关

#pragma mark -- UICollectionViewDataSource 数据源方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_collview1==collectionView) {
        
        return CGSizeMake((DEVW-40)/4 ,50);
    }else
    {
        return CGSizeMake(DEVW ,130);
    }

}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (collectionView==_collview2) {
        
           return UIEdgeInsetsMake(0, 0, 0, 0);
        
    }else
    {
          return UIEdgeInsetsMake(0, 0, 0, 0);
    }
  
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _collview1==collectionView?colldate1.count:index_job_ads.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView==_collview1) {
        static NSString *ID = @"collCell1";
        NSDictionary*tempdic=menu[indexPath.row];
        collCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        NSString*str= colldate1[indexPath.row];
        NSString*imgstr=[NSString stringWithFormat:@"首页%@",str];
        [cell.img sd_setImageWithURL:[tempdic[@"image"]formatUrl] placeholderImage:WXGImage(imgstr)];
        cell.lb1.text=tempdic[@"name"];
        if (indexPath.row==11) {
            cell.lb1.text=str;
        }
        
        return cell;
    }else
    {
        static NSString *ID = @"collCell2";
        NSDictionary*dic=index_job_ads[indexPath.row];
        collCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        [cell.img sd_setImageWithURL:[[NSString stringWithFormat:@"%@%@",HOST,dic[@"image"]]formatUrl] placeholderImage:WXGImage(@"测试H1")];

        return cell;
    }
 
}

#pragma mark --UICollectionViewDelegate

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (collectionView==_collview1) {
        //更多
        if (indexPath.row==11) {
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"NewHome" bundle:nil];
            ShopHome2Controller*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"ShopHome2"];
            secondViewController.type=@"home";
            [self.navigationController pushViewController:secondViewController animated:YES];
            return;
        }else
        {
            [MyUtils collviewSelidex:@"兼职" toVc:self andShopInfo:menu[indexPath.row]];
        }

    }else
    {
         [MyUtils collviewSelidex:@"职位详情" toVc:self andShopInfo:index_job_ads[indexPath.row]];
    }
    
   
}





#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  mary.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    
    HomeTbvCell*cell=[HomeTbvCell CellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell Cellinit:mary[indexPath.row]];
    return cell;
    
}


#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    HomeTbvCell*cell=[tableView cellForRowAtIndexPath:indexPath];
   
 return   [HomeTbvCell FSCellHeightForTableView:tableView indexPath:indexPath cacheKey:@"HomeTbvCell" cellContentViewWidth:0 bottomOffset:0];
//    return 150+cell.collview.contentSize.height;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [MyUtils collviewSelidex:@"职位详情" toVc:self andShopInfo:mary[indexPath.row]];
}
- (IBAction)searchBtna:(UIButton *)sender {
      [MyUtils collviewSelidex:@"搜索" toVc:self andShopInfo:nil];
    
}

@end
